﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Fighter.Screens;
using Microsoft.Xna.Framework.Input;

namespace Fighter.GameContent
{
    class Sword
    {
        #region const
        /// <summary>
        /// FLOAT : Puissance du coup d'épée. Correspond au nb de PX du recul.
        /// </summary>
        private const float touchVelocity = 157;

        #endregion


        private Animation swordAnimation;
        private GraphicsDeviceManager graphics;

        private Level level;
        private Vector2 playerPosition;
        private Rectangle localBounds;
        private SpriteEffects flip = SpriteEffects.None;
        private AnimationPlayer sprite;
        private SoundEffect touchSound;

        public Boolean state { get; set; }
        /// <summary>
        /// BOOL : Si l'ennemi à déjà été touché au moins une fois pendant l'attaque
        /// </summary>
        public Boolean alreadyHitted { get; set; } 


        public Vector2 Position
        {
            get { return position; }
        }
        Vector2 position;

        /// <summary>
        /// Gets a rectangle which bounds this sword in world space.
        /// </summary>
        public Rectangle BoundingRectangle
        {
            get
            {
                int left = (int)Math.Round(Position.X - sprite.Origin.X) + localBounds.X;
                int top = (int)Math.Round(Position.Y - sprite.Origin.Y) + localBounds.Y;

                return new Rectangle(left, top, localBounds.Width, localBounds.Height);
            }
        }

        /// <summary>
        /// Construct a new Sword.
        /// </summary>
        public Sword(Level level, Vector2 position, GraphicsDeviceManager graphics)
        {
            this.level = level;
            this.position = position;
            //this.position.X = this.position.X + 500;
            this.graphics = graphics;
            this.playerPosition = new Vector2(position.X, position.Y);
            LoadContent();
            ScreenManagers.StateManager.CurrentGameState = ScreenManagers.StateManager.GameState.Alive;
            this.state = true;
        }

        /// <summary>
        /// Loads the sword sprite.
        /// </summary>
        public void LoadContent()
        {
            swordAnimation = new Animation(this.level.Content.Load<Texture2D>("Sprites/Weapon/Sword2"), 0.1f, false);

            // Calculate bounds within texture size.
            int width = 70;//(int)(swordAnimation.FrameWidth * 0.2 * GameplayScreen.baseScreenSize.X / (float)graphics.GraphicsDevice.PresentationParameters.BackBufferWidth);
            int left = (swordAnimation.FrameWidth - width) / 5;
            int height = 145;//(int)(swordAnimation.FrameWidth * GameplayScreen.baseScreenSize.Y / (float)graphics.GraphicsDevice.PresentationParameters.BackBufferHeight);
            int top = swordAnimation.FrameHeight - height;
            localBounds = new Rectangle(left, top, width, height);
           
            sprite. PlayAnimation(swordAnimation);

            //load sounds
            touchSound = this.level.Content.Load<SoundEffect>("Game/Sounds/ouch");
        }

        public void Update(GameTime gameTime)
        {
            
            
            HandleCollisions();
            
        }

        private void HandleCollisions()
        {
            // Get the player's bounding rectangle and find neighboring tiles.
            Rectangle bounds = BoundingRectangle;
            int leftTile = (int)Math.Floor((float)bounds.Left / Tile.Width);
            int rightTile = (int)Math.Ceiling(((float)bounds.Right / Tile.Width)) - 1;
            int topTile = (int)Math.Floor((float)bounds.Top / Tile.Height);
            int bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / Tile.Height)) - 1;

            
        }

        public void OnHitEnemy(Enemy e, Player p)
        {
            if (!this.alreadyHitted)
            {
                e.Life -= 2;
                this.alreadyHitted = true;

                if (p.Orientation < 0)
                {
                    e.iveBeenHit(-touchVelocity);
                }
                else
                {
                    e.iveBeenHit(touchVelocity);
                }
            }
            touchSound.Play();

        }

        /// <summary>
        /// Draws the animated weapon.
        /// </summary>
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, Player owner)
        {
            Vector2 pos = owner.Position;

            if (sprite.FrameIndex == (swordAnimation.FrameCount - 1))
                this.state = false;


            if (this.state == true)
            {
                if (owner.Orientation < 0)
                {
                    pos.X = pos.X - 45;
                    flip = SpriteEffects.FlipHorizontally;
                }
                else
                {
                    pos.X = pos.X + 45;
                    flip = SpriteEffects.None;
                }


                sprite.Draw(gameTime, spriteBatch, pos, flip);
            }

            
        }
    }
}
